import React from 'react'
import {connect} from 'react-redux'
import {messages} from "share/common";
import { Form, Input, Switch, Button, message } from 'antd'
import auditOpinion from 'containers/finance-setting/audit-opinion/audit-opinion.service'
const { TextArea } = Input;
const FormItem = Form.Item;
const formItemLayout = {
  labelCol: {span: 8},
  wrapperCol: {span: 14},
};

class EditOpinion extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      saveLoading: false,
      mode: ''
    }
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.editSlideShow === this.props.editSlideShow)
      return;
    if (!nextProps.editSlideShow) {
      this.props.form.resetFields();
      return !1;
    }
    this.setState({
      mode: nextProps.mode
    })
  }

  //保存
  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if(!err){
        this.setState({saveLoading:true});
        if (this.state.mode === 'child') {
          let params = {};
          params.reply = values.reply;
          // params.status = values.status ? 1000 : 1001;
          params.id = this.props.content.id;
          auditOpinion.editChildReply(params).then(() => {
            this.setState({saveLoading:false});
            this.props.close(true);
          }).catch(() => {
            this.setState({saveLoading:false});
            message.error(messages('finance-setting.key65'/*出错了*/));
          })
        } else {
          let params = {};
          params.reply = values.reply;
          params.status = values.status ? 1000 : 1001;
          params.id = this.props.content.id;
          auditOpinion.editReply(params).then(() => {
            this.setState({saveLoading:false});
            this.props.close(true);
          }).catch(() => {
            this.setState({saveLoading:false});
            message.error(messages('finance-setting.key65'/*出错了*/));
          })
        }
      }
    });
  };

  render(){
    const {saveLoading, mode} = this.state;
    const {getFieldDecorator} = this.props.form;
    const { content } = this.props;
    return(
      <div>
        <Form onSubmit={this.handleSave} style={{marginTop:'10px',fontSize:'14px'}}>
          {mode === 'child' ? '' : <FormItem className="priority-wrap"
                    label={messages('common.column.status')/*状态*/} {...formItemLayout}>
            {getFieldDecorator('status', {
              rules: [{
                required: true, message: messages('common.please.input')/*请输入*/,
              }],
              initialValue: content && content.status && content.status === 1000 ? true : false,
              valuePropName: 'checked'
            })(
              <Switch />
            )}
          </FormItem>}
          <FormItem className="priority-wrap"
                    label={messages('finance-setting.key64'/*审核意见*/)} {...formItemLayout}>
            {getFieldDecorator('reply', {
              rules: [{
                required: true, message: messages('common.please.input')/*请输入*/,
              },{
                max: 200,
                message: messages('finance-setting.key69'/*审核意见最多不超过200个字符*/)
              }],
              initialValue: content && content.reply
            })(
              <TextArea placeholder={messages('finance-setting.key70'/*请输入，最多200字符*/)} style={{height:'150px'}}/>
            )}
          </FormItem>
        </Form>
        <div className="footer-operate" style={{backgroundColor:'#ffffff',left:'0px'}}>
          <Button type="primary"  onClick={this.handleSave} style={{margin:'0px 10px'}} loading={saveLoading}>{messages('common.save')/*保存*/}</Button>
          <Button onClick={this.props.close}>{messages('common.cancel')/*取消*/}</Button>
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  }
}
const wrappedEditOpinion = Form.create()(EditOpinion);
export default connect(mapStateToProps)(wrappedEditOpinion);
